﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TheVerge.Base;
using System.Windows.Media;
using TheVerge.Base.Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Collections.Generic;

namespace TheVerge.Phone.Controls
{
    public partial class MediaControl : UserControl
    {
        public bool IsViewingMedia
        {
            get { return !AppBarHelper.IsVisible; }
            private set { AppBarHelper.IsVisible = !value; }
        }

        public MediaControl()
        {
            InitializeComponent();
        }

        public void LoadFromQueryStrings(Dictionary<string, string> queryStrings)
        {
            if (queryStrings.ContainsKey(VMMedia.ACTIVE_SECTION_QS))
            {
                var section = queryStrings[VMMedia.ACTIVE_SECTION_QS];
                var media = App.MainVM.Media;


                foreach (var piv in mediaPiv.Items)
                {
                    var p = piv as PivotItem;
                    var vm = p.DataContext as IVMEntries;

                    if (vm.DisplayName == section)
                        mediaPiv.SelectedItem = piv;
                }
            }
        }

        private void PhotoGallery_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var g = fe.DataContext as GalleryEntry;

            App.MainVM.ActiveGallery = g;

            AppBarHelper.LaunchIfDifferentPage(@"/Pages/GalleryPage.xaml");
        }

        private void mediaPiv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mediaPiv.SelectedItem != null)
            {
                var piv = mediaPiv.SelectedItem as PivotItem;
                var vm = piv.DataContext as IVMEntries;
                if (vm != null && vm.ItemsCount == 0)
                    vm.Refresh();

                App.MainVM.Media.ActiveSection = vm;
            }
        }

        private void PodcastPlay_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var podcast = button.DataContext as VMPodcast;

            podcast.PlayPause();
        }

        private void PodcastSelect_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var podcast = fe.DataContext as VMPodcast;

            podcast.Select();

            AppBarHelper.LaunchIfDifferentPage(@"/Pages/PodcastPage.xaml");
        }

        private void Video_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var fe = sender as FrameworkElement;
                var video = fe.DataContext as VideoEntry;

                if (string.IsNullOrEmpty(video.VideoUrl)) return;

                var mp = new MediaPlayerLauncher();
                mp.Media = new Uri(video.VideoUrl, UriKind.Absolute);
                mp.Controls = MediaPlaybackControls.None;
                mp.Show();
            }
            catch
            {
                MessageBox.Show("An error occured when attempting to load the video.");
            }
        }

        private void VideoUrl_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var video = fe.DataContext as VideoEntry;

            if (string.IsNullOrEmpty(video.Url)) return;

            var wbt = new WebBrowserTask();
            wbt.Uri = new Uri(video.Url, UriKind.Absolute);
            wbt.Show();
        }
    }
}
